package pod

import (
	"krm-backend/config"
	"krm-backend/utils/logs"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
)

// 定义upgrader用于升级http请求
var upgrader = websocket.Upgrader{
	ReadBufferSize:  1024,
	WriteBufferSize: 1024,
	CheckOrigin: func(r *http.Request) bool {
		return true
	},
}

func CreateWS(r *gin.Context) (ws *websocket.Conn) {
	returnData := config.ReturnData{}
	//升级request为websocket协议
	ws, err := upgrader.Upgrade(r.Writer, r.Request, nil)
	if err != nil {
		//说明ws升级失败
		logs.Error(map[string]interface{}{"msg": err.Error()}, "websocket升级失败")
		returnData.Message = err.Error()
		returnData.Status = 400
		r.JSON(http.StatusOK, returnData)
		return
	}
	return
}
